<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Db;

class role extends Base
{
    protected $id = 'role_id';
    protected $table = 'role';
    protected $join = ['area a','a.area_id = m.area_id','left'];

    public function users() {

        $id = input('param.role_id');
        $list = Db::name('user')->select();
        $this->assign('list',$list);

        return $this->out();

    }

    protected function options( $item = [] ) {

        $ztree = Db::name('menu')->where('module_type', 0)->field('id,name,pid AS pId')->select();
        $ztree[0]['open'] = true;

        $item['menus'] = explode(',',$item['menus']);
        foreach($ztree AS $id => $z) {
            if(in_array($z['id'],$item['menus'])) {
                $ztree[$id]['checked'] = true;
            }
        }
        $this->assign('zTree',json_encode($ztree));

        //
        $areas = Db::name('area')->whereNull('delete_time')->column('*','area_id');
//		$area_ids = Arr2Sibling($areas,0,0,'area_id','area_pid');
        $area_ids = $areas;
        $this->assign('area_ids', TplOptions($area_ids, $item['area_id'],'area_name', 'area_id') );


        return $item;

    }

    //搜索
    protected function search() {

        $P = input('param.');
        $where = [];

        $P['role_name'] && $where['role_name'] = ['LIKE',"%{$P['role_name']}%"];
        $P['area_id'] && $where['area_id'] = ['=',$P['area_id']];

        return $where;
    }
}
